#!/bin/bash
#
# Pulley routines and globals for managing probes
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 19 November 2013
#
#   Copyright 2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.


declare -a -g P_PROBES


## validate_probe <probe name>
##
## Checks to see if <probe name> has been loaded. Returns 0 if the probe
## has been enabled, non-zero otherwise.
##
function validate_probe() {
	is_element "$1" "${P_PROBES[@]}"
	return $?
}


## probe_summary <probe name>
##
## Echoes summary information for <probe name>. Returns 0 upon success. If
## <probe name> is unknown or has not been enabled, echoes an error message
## and returns 2.
##
function probe_summary() {
	local status=0
	
	if validate_probe "$1"; then
		p_probe_$1_summary
		status=$?
	else
		echo "Probe unknown or not enabled: $1"
		status=2
	fi
	
	return ${status}
}


## probe_help <probe name>
##
## Echoes help information for <probe name>. Returns 0 upon success. If
## <probe name> is unknown or has not been enabled, echoes an error message
## and returns 2.
##
function probe_help() {
	local status=0
	
	if validate_probe "$1"; then
		p_probe_$1_help
		status=$?
	else
		echo "Probe unknown or not enabled: $1"
		status=2
	fi
	
	return ${status}
}


## run_probe <probe name>
##
## Runs the probe specified by <probe name>. If <probe name> is unknown or
## not enabled, returns 2. Otherwise, returns the exit status of the probe.
##
function run_probe() {
	local status=0
	
	if validate_probe "$1"; then
		p_probe_$1
		status=$?
	else
		echo "Probe unknown or not enabled: $1"
		status=2
	fi
	
	return ${status}
}


## run_probes
##
## Runs all enabled probes. If an error is encountered, saves the exit
## status of the first error and continues executing the remaining probes.
##
## Returns 0 if all probes were successfully run, non-zero if an error
## occurred. The error status will be the exit status of the first probe
## that encountered an error.
##
function run_probes() {
	local status=0
	
	for probe in "${P_PROBES[@]}"; do
		run_probe "${probe}"
		status=$(merge_status ${status} $?)
	done
	
	return ${status}
}


## load_probe <probe name or path> [probe name or path ...]
##
## Loads the probe specified by <probe name or path>. If a probe file with
## the name <probe name>.probe is located in the probes subdirectory of the
## Pulley library directory, then that file will be sourced. Otherwise, this
## function will attempt to source the file given as an argument. Returns 0
## on success, non-zero otherwise.
##
function load_probe() {
	local status=0
	local chk=0
	
	for arg in "$@"; do
		if [[ -f "${P_LIBDIR}/probes/$1.probe" ]]; then
			. "${P_LIBDIR}/probes/$1.probe"
			status=$(merge_status ${status} $?)
		else
			if [[ -f "$1" ]]; then
				. "$1"
				status=$(merge_status ${status} $?)
			else
				echo "Probe or file not found: $1"
				status=2
			fi
		fi
	done
	
	return ${status}
}


## enable_probe <probe name>
##
## Enables a probe named <probe name>. If the probe's execution function
## is not defined, also tries to load the probe with load_probe.
##
function enable_probe() {
	local status=0
	
	if is_function "p_probe_$1"; then
		P_PROBES+=("$1")
	else
		load_probe "$1"
		status=$?
		
		if ((status == 0)); then
			P_PROBES+=("$1")
		fi
	fi
	
	return ${status}
}
